# 字典类型
import re
from datetime import datetime

from flask_jwt_extended import get_jwt_identity
from flask_restful import abort
from sqlalchemy.orm import validates

from apps.model.baseModel import BaseModel
from apps.model.serializeModel import model_to_list
from apps.settingApi.api.users.model import usersModel
from comm.sqlalchemyConfig import db


class basicTypeModel(BaseModel):
    """
    基础数据类型表
    """
    __tablename__ = 'tx_baeic_type'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    label = db.Column(db.String(200), nullable=False,unique=True,doc='描述')
    value = db.Column(db.String(100), nullable=False,unique=True,doc='名称值')
    str01 = db.Column(db.String(500), nullable=True, doc='字符01')
    str02 = db.Column(db.String(500), nullable=True, doc='字符02')
    str03 = db.Column(db.String(500), nullable=True, doc='字符03')
    qty01 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字01')
    qty02 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字02')
    qty03 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字03')
    rem01 = db.Column(db.Text, nullable=True,default='',doc='备注01')
    rem02 = db.Column(db.Text, nullable=True,default='',doc='备注02')
    rem03 = db.Column(db.Text, nullable=True,default='',doc='备注03')
    sort = db.Column(db.Integer, nullable=True,default=0,doc='排序')
    status = db.Column(db.Boolean, nullable=True, default=1, doc='是否启用')
# 字典数据
class basicDataModel(BaseModel):
    """
    基础数据表
    """
    __tablename__ = 'tx_basic_data'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    # label = db.Column(db.String(125), nullable=False,unique=True,doc='描述')
    label = db.Column(db.String(125), nullable=False,doc='描述')
    value = db.Column(db.String(125), nullable=False,doc='名称值')
    parent_id = db.Column(db.String(125), nullable=True,doc='所属上级')
    basic_value = db.Column(db.String(100), db.ForeignKey('tx_baeic_type.value'))
    str01 = db.Column(db.String(500), nullable=True, doc='字符01')
    str02 = db.Column(db.String(500), nullable=True, doc='字符02')
    str03 = db.Column(db.String(500), nullable=True, doc='字符03')
    str04 = db.Column(db.String(500), nullable=True, doc='字符04')
    str05 = db.Column(db.String(500), nullable=True, doc='字符05')
    str06 = db.Column(db.String(500), nullable=True, doc='字符06')
    str07 = db.Column(db.String(500), nullable=True, doc='字符07')
    str08 = db.Column(db.String(500), nullable=True, doc='字符08')
    str09 = db.Column(db.String(500), nullable=True, doc='字符09')
    str10 = db.Column(db.String(500), nullable=True, doc='字符10')
    qty01 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字01')
    qty02 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字02')
    qty03 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字03')
    qty04 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字04')
    qty05 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字05')
    qty06 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字06')
    qty07 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字07')
    qty08 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字08')
    qty09 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字09')
    qty10 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字10')
    rem01 = db.Column(db.Text, nullable=True, default='', doc='备注01')
    rem02 = db.Column(db.Text, nullable=True, default='', doc='备注02')
    rem03 = db.Column(db.Text, nullable=True, default='', doc='备注03')
    rem04 = db.Column(db.Text, nullable=True, default='', doc='备注04')
    rem05 = db.Column(db.Text, nullable=True, default='', doc='备注05')
    rem06 = db.Column(db.Text, nullable=True, default='', doc='备注06')
    rem07 = db.Column(db.Text, nullable=True, default='', doc='备注07')
    rem08 = db.Column(db.Text, nullable=True, default='', doc='备注08')
    rem09 = db.Column(db.Text, nullable=True, default='', doc='备注09')
    rem10 = db.Column(db.Text, nullable=True, default='', doc='备注10')
    int01 = db.Column(db.Integer, nullable=True, doc='整数01')
    int02 = db.Column(db.Integer, nullable=True, doc='整数02')
    int03 = db.Column(db.Integer, nullable=True, doc='整数03')
    int04 = db.Column(db.Integer, nullable=True, doc='整数04')
    int05 = db.Column(db.Integer, nullable=True, doc='整数05')
    int06 = db.Column(db.Integer, nullable=True, doc='整数06')
    int07 = db.Column(db.Integer, nullable=True, doc='整数07')
    int08 = db.Column(db.Integer, nullable=True, doc='整数08')
    int09 = db.Column(db.Integer, nullable=True, doc='整数09')
    int10 = db.Column(db.Integer, nullable=True, doc='整数10')
    start_time = db.Column(db.DateTime, default=datetime.now, nullable=True, doc='启用日期')
    end_time = db.Column(db.DateTime, default=datetime.now, nullable=True, doc='停用日期')
    sort = db.Column(db.Integer, nullable=True, default=0, doc='排序')

    @validates('value')
    def validate_address(self, key, username):
        # existData = self.__class__.query.filter_by(username=username).first()
        # if existData != None:
        #     abort(400, msg='用户名重复')
        pattern = r'^[a-zA-Z0-9\W]+$'
        if bool(re.match(pattern, username))==False:
            abort(400, msg='编码只能包含英文、符号、数字')
        return username

    @classmethod
    def getPoolPk(cls,pk):
        try:
            pkData = db.session.query(cls).filter(cls.id == pk).first()
        except Exception as e:
            return None, repr(e)
        if pkData==None:
            return None,'连接池不存在' + pk
        odjData = {
            "DBTYPE": pkData.str03,
            "USER": pkData.label,
            "HOST": pkData.str02,
            "PORT": pkData.str05,
            "NAME": pkData.str04,
            "PASSWORD": pkData.str01,
            "DRIVER": pkData.str06,
            "MULTIPLEDB": [],
        }
        return odjData,None